﻿var PlayScene = cc.Scene.extend({
    bg: null,
    hero: null,
    touchY: null,
    _coffeeEffect: null,

    foodManager: null,
    pattern: 1,//1代表随机， 2代表 垂直
    gameScene: null,//代表游戏主场景
    foods: null,

    onEnter:function() {
        cc.spriteFrameCache.addSpriteFrames("res/graphics/texture.plist");
        this._super();
        this.bg = new Background();
        this.addChild(this.bg);
        this.hero = new HugryHero();
        this.hero.x = 400;
        this.hero.y = 400;
        this.addChild(this.hero);
        this.foodManager = new FoodManager(this);
        this.init();
        this.addEvent();
        this.scheduleUpdate();
        //测试
        this.test();
        
    },
    init: function() {
        this.hero.x = 0;
        this.hero.y = 0;
        this.bg.speed = 0;
        Game.State = GameConstant.Game_State_IDLE;
    },
    update:function(elapsed) {
        switch (Game.State) {
            case GameConstant.Game_State_IDLE:
                this.hero.setRotation(-45);
                var size = cc.director.getWinSize();
                this.bg.speed += (GameConstant.Game_Min_Speed - this.bg.speed) * 0.01;
                this.hero.x += (size.width / 4 - this.hero.x) * 0.1;
                this.hero.y += (size.height / 2 - this.hero.y) * 0.1;
                if (this.hero.y > size.height / 2 - 0.02) {
                    this.hero.setRotation(0);
                    Game.State = GameConstant.Game_State_FLY;
                }
                break;
            case GameConstant.Game_State_FLY:
                if (Game.User.Coffer > 0) {
                    this.showCoffeeEffect();
                    this._coffeeEffect.y += (this.touchY - this.hero.y) * 0.3;
                    this.bg.speed += (GameConstant.Game_Max_Speed - this.bg.speed) * 0.2;
                } else {
                    this.bg.speed += (GameConstant.Game_Min_Speed - this.bg.speed) * 0.2;
                    this.stopCoffeeEffect();
                }
                if (Game.User.Coffer > 0)
                    Game.User.Coffer -= elapsed;
                this.hero.y += (this.touchY - this.hero.y) * 0.3;
                this.handlePose();
                
            break;
            case GameConstant.Game_State_OVER:
                this.bg.speed += (0 - this.bg.speed) * 0.01;
                this.hero.x += (0 - this.hero.x) * 0.1;
                this.hero.y += (0 - this.hero.y) * 0.01;
                this.hero.setRotation(45);
                this.stopCoffeeEffect();
            break;
        }
        this.foodManager.update1(this.hero, elapsed);
    },
    addEvent: function() {
        cc.eventManager.addListener({
                event: cc.EventListener.MOUSE,
                onMouseMove: function(event) {
                    var pos = event.getLocation();
                    this.touchY = pos.y;
                    console.log("loc" + pos.y);
                }.bind(this)
            },
            this);
    },
    handlePose: function () {
        var winSize = cc.director.getWinSize();
        var angle = (this.hero.y - this.touchY) * 0.5;
        this.hero.setRotation(angle);
        if (this.hero.y < this.hero.height * 0.5) {
            this.hero.y = this.hero.height * 0.5;
            this.hero.setRotation(0);
        }
        if (this.hero.y > winSize.height - this.hero.height * 0.5) {
            this.hero.y = winSize.height - this.hero.height * 0.5;
            this.hero.setRotation(0);
        }
    },
    test:function() {
        var item1 = new cc.MenuItemFont("开始", this.fast, this);
        item1.x = 100;
        var item2 = new cc.MenuItemFont("死亡", this.normal, this);
        item2.x = 200;
        var item3 = new cc.MenuItemFont("粒子", this.testCoffee, this);
        item3.x = 300;
        var menu = new cc.Menu(item1, item2, item3);
        this.addChild(menu);
    },
    fast: function() {
        this.init();
    },
    normal: function () {
        Game.State = GameConstant.Game_State_OVER;
    },
    testCoffee: function() {
        Game.User.Coffer = 5;
    },
    showCoffeeEffect:function() {
        if (this._coffeeEffect)
            return;
        this._coffeeEffect = new cc.ParticleSystem(res.coffee_plist);
        this.addChild(this._coffeeEffect);
        this._coffeeEffect.x = this.hero.x + this.hero.width / 4;
        this._coffeeEffect.y = this.hero.y;
    },
    stopCoffeeEffect:function() {
        if (this._coffeeEffect) {
            this._coffeeEffect.stopSystem();
            this.removeChild(this._coffeeEffect);
            this._coffeeEffect = null;
        }
    }
    

});
